package com.zzty.txj1.controller;



import com.zzty.txj1.entity.SelfExamTransaction;
import com.zzty.txj1.entity.TeamExamTransaction;
import com.zzty.txj1.entity.User;
import com.zzty.txj1.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/teamExamTransaction")
public class TeamExamTransactionController {

    @Autowired
    QuestionService questionService;
    @Autowired
    TeamExamTransactionService teamExamTransactionService;

    @Autowired
    private TeamExamService teamExamService;

    @RequestMapping(value = "/checkFirstLogIn")
    public TeamExamTransaction checkFirstLogIn(TeamExamTransaction teamExamTransaction, HttpServletRequest request){
        TeamExamTransaction teamExamTransaction1=new TeamExamTransaction();
        User user=(User)request.getSession().getAttribute("user");
        Integer groupId = teamExamTransactionService.getId(user.getName());
        System.out.println("======"+groupId);
        teamExamTransaction.setGroupId(groupId);
        teamExamTransaction1=teamExamTransactionService.checkFirstLogIn(teamExamTransaction);
        return teamExamTransaction1;
    }


    @RequestMapping("/initTeamExamTransaction")
    public Integer initTeamExamTransaction(TeamExamTransaction teamExamTransaction,HttpServletRequest request){

        System.out.println("initTeamExamTransaction===========");
        User user=(User)request.getSession().getAttribute("user");
        Integer groupId = teamExamTransactionService.getId(user.getName());
        System.out.println("======"+groupId);
        teamExamTransaction.setGroupId(groupId);
        Date d=new Date();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date=sdf.format(d);
        teamExamTransaction.setStartTime(date);
        System.out.println("teamExamTransaction==="+teamExamTransaction);
        Integer flag=teamExamTransactionService.initTeamExamTransaction(teamExamTransaction);
        System.out.println("========"+flag);
        return flag;
    }

    @RequestMapping("/getStartTime")
    public String getStartTime(TeamExamTransaction teamExamTransaction,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        Integer groupId = teamExamTransactionService.getId(user.getName());
        System.out.println("======"+groupId);
        teamExamTransaction.setGroupId(groupId);
        String startTime=teamExamTransactionService.getStartTime(teamExamTransaction);
        return startTime;
    }

    @RequestMapping(value = "/updateUrlSpendTime",method = RequestMethod.POST)
    public Integer updateUrlSpendTime(TeamExamTransaction teamExamTransaction,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        String answerUrl=(String) request.getSession().getAttribute("answerUrl");
        Integer groupId = teamExamTransactionService.getId(user.getName());
        System.out.println("======"+groupId);
        teamExamTransaction.setGroupId(groupId);
        teamExamTransaction.setAnswerUrl(answerUrl);
        Integer flag=teamExamTransactionService.updateUrlSpendTime(teamExamTransaction);
        return flag;
    }
}
